package com.zhangxiaofanfan.hotcode.fourth.third;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 *
 *
 * @date 2024-06-29 03:21:25
 * @author zhangxiaofanfan
 */
public class HotCode069 {
    public static void main(String[] args) {
        HotCode069 hotCode = new HotCode069();
        String s = "{[()]}";
        System.out.println(hotCode.isValid(s));
    }

    private Map<Character, Character> map = new HashMap<>(
            Map.of(')', '(' , ']', '[', '}', '{')
    );
    public boolean isValid(String s) {
        if (s == null || s.isEmpty() || s.length() % 2 == 1) {
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (char curChar : s.toCharArray()) {
            if (!map.containsKey(curChar)) {
                stack.push(curChar);
            } else {
                if (stack.isEmpty() || map.get(curChar) != stack.pop()) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
